/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cognitivetouch.colorSpan;

import cognitivetouch.MainCW;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

/**
 *
 * @author Xiaoxing
 */
public class ScreenLib {

    private int screenWidth;
    private int screenHight;

    public void initScreen() {
        DisplayMode dm = getDisplayModes();
        screenHight = dm.getHeight();
        screenWidth = dm.getWidth();
        createDisplay(dm);
    }

    private DisplayMode getDisplayModes() {
        try {
            DisplayMode[] modes;
            modes = Display.getAvailableDisplayModes();
//            int maxResolution = 0;
//            DisplayMode max = null;
//            for (DisplayMode dm : modes) {
//                if (dm.getWidth() > maxResolution) {
//                    maxResolution = dm.getWidth();
//                    max = dm;
//                }
//            }
//            return max;
            for (DisplayMode dm:modes){
                if(dm.getWidth()==1600){
                    return dm;
                }
            }
        } catch (LWJGLException ex) {
            Logger.getLogger(MainCW.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    private void createDisplay(DisplayMode dm) {
        try {
            Display.setDisplayMode(dm);
            Display.setFullscreen(true);
            Display.create();
        } catch (LWJGLException e) {
            System.out.println(e.toString());
//            e.printStackTrace();
            System.exit(0);
        }
    }

    public void initOpenGL() {

        // init OpenGL
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, screenWidth, 0, screenHight, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
    }

    public int getCenterX() {
        return screenWidth / 2;
    }

    public int getCenterY() {
        return screenHight / 2;
    }

    public void glClear() {
        // Clear the screen and depth buffer
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    }
}
